//
//  BPMainViewController.swift
//  BPWeiBo
//
//  Created by apple on 16/4/6.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class BPMainViewController: UITabBarController {
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置控制器的tabbar为我们自定义的TabBar
        let tabBar = BPTabBar()
        
        //利用KVC将我们系统的只读的tabbar覆盖,换上自定义的tababr
        setValue(tabBar, forKey: "tabBar")
        
        //给安妮添加点击事件
        tabBar.plusBtn.addTarget(self, action:Selector("addBtnDid"), forControlEvents: .TouchUpInside)
        
        addChildViewController()

    }
    
    //MARK:添加按钮的点击事件
    @objc private func addBtnDid() {
        let appearView = BPAppearView()
        
        appearView.show(self)
        //UIApplication.sharedApplication().keyWindow?.addSubview(appearView)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    //MARk:添加子视图控制器
    //'private' 只有在当前文件才可以访问
    private func addChildViewController() {
        addChildViewCOntroller(BPHomeViewController(), title: "首页", imgName: "tabbar_home",index: 1)
        addChildViewCOntroller(BPMessageViewController(), title: "消息", imgName: "tabbar_message_center",index: 2)
        addChildViewCOntroller(BPDiscoverViewController(), title: "发现", imgName: "tabbar_discover",index: 3)
        addChildViewCOntroller(BPProfileTableViewController(), title: "我", imgName: "tabbar_profile",index: 4)
    }
    
    private func addChildViewCOntroller(vc: UIViewController, title: String, imgName: String,index: Int) {
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imgName)
        //设置选中的图片
        vc.tabBarItem.selectedImage = UIImage(named: imgName +  "_selected")?.imageWithRenderingMode(UIImageRenderingMode(rawValue: 1)!)
        //设置文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        vc.tabBarItem.tag = baseTag + index
        //添加导航控制器
        //let nav = UINavigationController(rootViewController: vc)
        let nav = BPNavViewController.init(rootViewController: vc)
        addChildViewController(nav)
    
    }
    
    //MARK:遍历查找我们tabBarItem上面对应的图片,当我们当我们选中某一个item的时候,找到图片对应的视图,让这个视图县先放大在缩小
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        //super.tabBar(tabBar, didSelectItem: item)
        
        var index = 1
        
        for subView in tabBar.subviews {
        
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                
                //定位到点击的视图
                if index == item.tag - baseTag {
                for subView1 in subView.subviews {
                    
                    if subView1.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                        
                        //到这里才是真正获取到了item上的视图
                        subView1.transform = CGAffineTransformMakeScale(0.5, 0.5)
                        //先缩小在放大\
                        UIView.animateWithDuration(0.25, animations: { 
                            subView1.transform = CGAffineTransformMakeScale(1, 1)
                            }, completion: { (_) in
                                //print("OK")
                        })
                        
                        
                    }
                    
                }
            }
                index += 1
                
                
            
            }
        }
    }


}
